from fastapi import FastAPI, Query
from fastapi.responses import RedirectResponse
from langserve import add_routes

from fastapi.responses import StreamingResponse
from fastapi.middleware.cors import CORSMiddleware
from .chains.langchain_my_tool import agent
from .chains.langchain_my_view import getResponse
import asyncio
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles

# 定义输入和输出类型（可选）
from pydantic import BaseModel

app = FastAPI()
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
    expose_headers=["*"],
)

import os
print(os.getcwd())

app.mount("/static", StaticFiles(directory="static"), name="static")

@app.get("/run_graph")
def run_graph(input: str = Query(..., description="输入内容"),session: str = Query(..., description="会话ID")):
    return StreamingResponse(getResponse(input,session), media_type="text/plain")

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8080)

# class Input(BaseModel):
#     input: str
#
#
# class Output(BaseModel):
#     output: str

# @app.get("/")
# async def redirect_root_to_docs():
#     return RedirectResponse("/docs")

# Edit this to add the chain you want to add
# add_routes(app, agent.with_types(input_type=Input, output_type=Output), path="/qwen3")